package com.collections.www;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class StudentTest {
    public static void main(String[] args) {
        //test01();
        test02();
    }

    /**
     * 集合工具类：Comparator接口（匿名内部类方式，暂不作要求）
     */
    public static void test01(){
//     创建一个list数组
        List<Student>lst0=new ArrayList<>();
        //添加元素
        lst0.add(new Student("zhangsan","nan",1));
        lst0.add(new Student("lisi","nan",2));
        lst0.add(new Student("wangwu","nan",3));

        // 排序算法
        System.out.println("使用集合工具类的sort方法进行排序：");
        Collections.sort(lst0, new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                int iVal=0;
                if (o1!=null&&o2!=null){
                    if (o1.getId()>o2.getId()){
                        iVal=1;
                    }else if (o1.getId()<o2.getId()){
                        iVal=-1;
                    }
                }
          return  iVal*-1;
        }});
        System.out.println("List排序之后的内容");
        printList(lst0);
    }

    /**
     * 集合工具类：Comparator接口（匿名外部部类方式，暂不作要求）
     */
    private static void test02() {
        // 创建一个List数组
        List<Student> lst0 = new ArrayList<>();
        // 添加元素
        lst0.add(new Student("zhangsan", "150001", 70));
        lst0.add(new Student("lisi", "150002", 60));
        lst0.add(new Student("wangwu", "150003", 90));
        // 排序算法
        System.out.println("使用集合工具类的sort方法进行排序：");
        Collections.sort(lst0,new StudentComparator());
        System.out.println("List排序之后的内容为：");
        printList(lst0);
    }
    private static  void printList(List<Student>lst){
        if (lst!=null){
            for (Object object:lst){
                System.out.println(object);
            }
        }
    }
}
//实现的了comparable 则说明student实现的了比较的行为能力
//实现student排序的业务逻辑
class Student implements Comparable<Student>{
   String name;
   String sex;
   int id;

    @Override
    public int compareTo(Student stu) {
        int reVal=0;
        if (this.getId()<stu.getId()){
            return 1;
        }else if (this.getId()<stu.getId()){
            return -1;
        }
        return reVal*-1;
    }
    @Override
    public String toString() {
        return "Student{" +
                "Name='" + name + '\'' +
                ", Sex='" + sex + '\'' +
                ", Id=" + id +
                '}';
    }
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public Student(String name, String sex, int id) {

        this.name = name;
        this.sex = sex;
        this.id = id;
    }
}